<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of auto
 *
 * @author carlos
 */
class Model_Autoclass {
	
	public static function get_tables(){
		$result = DB::query(Database::SELECT, 'SHOW TABLES')->execute();
		foreach($result as $table){
			$return[] = current($table);
		}
		return $return;
	}
	public static function get_fields($table_name){
		$result = DB::query(Database::SELECT, "DESCRIBE `$table_name`")->as_object()->execute();
		foreach($result as $item){
			$return[] = array(
				"name"=>$item->Field,
				"type"=>self::translate_type($item->Type),
				"null"=>"YES"===$item->Null,
				"default"=>$item->Default,
			);
		}
		return $return;
	}
	
	public static function translate_type($type = null){
		switch($type){
			case "bigint"==substr($type,0,6):
			case "mediumint"==substr($type,0,9):
			case "tinyint"==substr($type,0,7):
			case "smallint"==substr($type,0,8):
			case "int"==substr($type,0,3):return "int";
			case "enum"==substr($type,0,4):
			case "varchar"==substr($type,0,7):return "string";
			default: return $type;
		}
	}

}

?>
